import web
import os

from config import *
from wmauth import *
from wmconfig import *

import stat
import time
class files:
    def GET(self):
        (user, edit) = getAccess(web.cookies())
        dldpath = os.path.join(ssPath, 'download')
        if not os.path.isdir(dldpath):
            os.mkdir(dldpath)
        files = []
        id = 0
        for fl in os.listdir(dldpath):
            status  = os.lstat(dldpath + '/' + fl)
            files.append(
                web.storage({
                	'id'           :  id,
                	'name'         :  fl,
                        'size'         :  status[stat.ST_SIZE],
                        'ctime'        :  time.ctime(status[stat.ST_CTIME]),
                        'mtime'        :  time.ctime(status[stat.ST_MTIME]),
                        'mode'         :  status[stat.ST_MODE],
                }))
            id += 1
        web.render('files.html')

    def POST(self):
        (user, edit) = getAccess(web.cookies())
        if not edit:
            wmtools.raiseError(2)
        inp = web.input()
        uploaded = inp.fh
        fname = inp.filename
        if uploaded:
            _fl = open( os.path.join(ssPath,
                                   'download',
                                   fname), 'w')
            _fl.write( uploaded )
            _fl.close()
        web.redirect(Config().cfg['c_abspath'] + '/files')
